/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 StdFailOverCoordinator.cpp. Created on: 15.09.2009 15:07:09
 */

#include "smartobjects/network/StdFailOverCoordinator.hpp"

namespace smartobjects {

const std::string StdFailOverCoordinator::COORDINATOR_NAME = "StdFailOverCoordinator";

StdFailOverCoordinator::StdFailOverCoordinator() : m_poFactory(NULL) {

}

StdFailOverCoordinator::~StdFailOverCoordinator() {
	resetManagedConnectionFactory();
}

const std::string& StdFailOverCoordinator::getName() {
	return COORDINATOR_NAME;
}

IEndpointCoordinator::ManagedConnectionStatus StdFailOverCoordinator::checkConnectionStatus(IManagedConnection* poConnection) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	if ( m_mConnections.find(poConnection)!=m_mConnections.end() ) {
		return CONNECTED;
	}
	else
		return DISCONNECTED;
}

void StdFailOverCoordinator::insertHandler(IManagedConnection* poConnection, ConnectionHandler* poHandler) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	ConnectionsMap::iterator itCon = m_mConnections.find(poConnection);
	if(itCon==m_mConnections.end()) {
		Handlers aHandlersPerConnection;
		aHandlersPerConnection.insert(poHandler);
		std::pair<IManagedConnection*, Handlers> oNewPair
			(poConnection, aHandlersPerConnection);
		m_mConnections.insert(oNewPair);
	}
	else
		itCon->second.insert(poHandler);

	std::pair<ConnectionHandler*, IManagedConnection*> oReversePair(poHandler, poConnection);
	m_mHandlers.insert(oReversePair);
}

void StdFailOverCoordinator::removeHandler(IManagedConnection* poConnection, ConnectionHandler* poHandler) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	ConnectionsMap::iterator itCon = m_mConnections.find(poConnection);
	if(itCon!=m_mConnections.end()) {
		itCon->second.erase(poHandler);
		if(itCon->second.empty()) {
			m_mConnections.erase(itCon);
		}
	}
	m_mHandlers.erase(poHandler);
}

IEndpointCoordinator::ManagedConnectionStatus StdFailOverCoordinator::onConnected(ConnectionHandler* poHandler) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );

	if(m_poFactory) {
		IManagedConnection* poConnection = m_poFactory->createManagedConnection();
		insertHandler(poConnection, poHandler);
		poConnection->assignConnectionHandler(poHandler);
		return checkConnectionStatus(poConnection);
	}
	else
		return DISCONNECTED;
}

IEndpointCoordinator::ManagedConnectionStatus StdFailOverCoordinator::onConnectionFailure(ConnectionHandler* poHandler, const std::string& sErrorMessage) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	HandlersMap::iterator itH = m_mHandlers.find(poHandler);
	if(itH!=m_mHandlers.end()) {
		return checkConnectionStatus(itH->second);
	}
	else
		return DISCONNECTED;
}

IEndpointCoordinator::ManagedConnectionStatus StdFailOverCoordinator::onDisconnected(ConnectionHandler* poHandler)  {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	HandlersMap::iterator itH = m_mHandlers.find(poHandler);
	if(itH!=m_mHandlers.end()) {
		IManagedConnection* poManagedConnection = itH->second;
		itH->second->unassignConnectionHandler(poHandler);
		removeHandler(itH->second, poHandler);
		return checkConnectionStatus(poManagedConnection);
	}
	else
		return DISCONNECTED;
}

IEndpointCoordinator::ManagedConnectionStatus StdFailOverCoordinator::onRemoved(ConnectionHandler* poHandler) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	return onDisconnected(poHandler);
}

bool StdFailOverCoordinator::onRestoreConnectionNeeded(ConnectionHandler* poHandler)  {
	return true;
}

ConnectionHandler* StdFailOverCoordinator::getConnectionHandlerToSend ( IManagedConnection* poConnection) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	ConnectionsMap::iterator itCon = m_mConnections.find(poConnection);
	if(itCon!=m_mConnections.end()) {
		ConnectionHandler* poResult = *itCon->second.begin();
		poResult->addRef();
		return poResult;
	}
	else
		throw IOException("Unable to found any available connections to send!");
}

void StdFailOverCoordinator::setManagedConnectionFactory(IManagedConnectionFactory* poFactory) {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	m_poFactory = poFactory;
}

void StdFailOverCoordinator::resetManagedConnectionFactory() {
	setManagedConnectionFactory(NULL);
}

IManagedConnectionFactory* StdFailOverCoordinator::getManagedConnectionFactory() {
	boost::recursive_mutex::scoped_lock ( m_mxCoordinator );
	return m_poFactory;
}


}
